package com.cdks.wuxiu.api.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ApplepayUtils {
    /**
     * URL
     */
    private static final String BAY_URL = "https://buy.itunes.apple.com/verifyReceipt";

    private static final String SANDBOX_URL = "https://sandbox.itunes.apple.com/verifyReceipt";

    /**
     * 验证支付收据
     *
     * @param receipt 交易收据
     * @return 验证结果
     */
    public static JSONObject verifyReceipt(String receipt, Boolean sandbox) {
        String url = BAY_URL;
        if (sandbox) {
            url = SANDBOX_URL;
        }
        Map<String, Object> body = new HashMap<>(5);
        body.put("receipt-data", receipt);
        String result = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(body))
                .execute()
                .body();
        log.info("苹果支付 result:" + result);
        return JSONUtil.parseObj(result);
    }
}
